今天來介紹述詞表,它是Django內建進階欄位查詢(field lookup)的用法,它可以讓查詢的範疇更廣,更主觀化的查詢方式。
格式如下:
<欄位名稱>__述詞 #底線*2哦!!
底下範例的欄位名稱可以請植物代理人查看
Classlist.objects.filter(number__exact=11)
Classlist.objects.filter(name__iexact='jim')
Classlist.objects.filter(name__contains ='陳')
Classlist.objects.filter(name__in=['乙班','戊班'])
Classlist.objects.filter(name__gt=10)
Classlist.objects.filter(name__gte=20)
Classlist.objects.filter(name__lt=26)
Classlist.objects.filter(name__lte=11)
Classlist.objects.filter(class__startswith='二年')
Classlist.objects.filter(number__startswith=1)
Classlist.objects.filter(Last__Modified_range=(datetime.date(2022,10,1),datetime.date(2022,10,3))
Classlist.objects.filter(Last_Modified__year=2022)
Classlist.objects.filter(Upload_Time__month=8)
Classlist.objects.filter(Upload_Time__day=10)
Classlist.objects.filter(Upload_Time__isnull=False)
大致上有這些述詞,在查詢資料時要好好善用它哦!
先到這裡,大家掰掰~
圖片來源:哈士吉